package com.rfsp.mcp.service;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rfsp.mcp.pojo.po.SmsPO;
import com.rfsp.common.service.ParamService;

@Service
public class SmsService {
	public Logger logger = LoggerFactory.getLogger(SmsService.class);
	
	@Autowired
	ParamService paramService;
	
	public String getSmsUrl() {
		String sms_service_url = paramService.getValue("sms_service_url") + "sendMsgRfpAndRfsp";
		return sms_service_url;
	}

    public String sendSms(SmsPO sms) {
        //从数据库中查询出url
        String url = this.getSmsUrl();
        HashMap<String,String> res = this.sendSms(url, sms.getContent(), sms.getSerialNumber(), sms.getMobile());
        return res.get(sms.getMobile());

    }
    
    public HashMap<String,String> sendSms(List<SmsPO> lSms) {
    	HashMap<String,String> res = new HashMap<String,String>(lSms.size());
        for(SmsPO sms : lSms) {
        	String rs = sendSms(sms);
        	res.put(sms.getMobile(), rs);
        }
        
        return res;

    }

	/**
	 * 序列号被重新定义，无法使用
	 * @param tel
	 * @param content
	 */
	@Deprecated
    public void sendSms(String tel, String content) {
        String url = this.getSmsUrl();
        this.sendSms(url, content, UUID.randomUUID().toString(), tel);

    }
	public void sendSms(String tel, String content,String serialNumber) {
		String url = this.getSmsUrl();
		this.sendSms(url, content,serialNumber, tel);

	}
	/**
	 * 获取短信回执
	 * @param sms_api_ums_url
	 * @return
	 */
	public String getReport(){
		String sms_service_url = paramService.getValue("sms_service_url") + "getReport";
		String info = null;
		try{
			HttpClient httpclient = new HttpClient();
			PostMethod post = new PostMethod(sms_service_url);
			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");
			httpclient.executeMethod(post);

			info = new String(post.getResponseBody(),"UTF-8");
			System.out.println(info);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return info;
	}

	/**
	 * 发短
	 * @param sms_service_url
	 * @param smsContent
	 * @param serialNumber
	 * @param phoneNumbers
	 * @return
	 */
	public HashMap<String,String> sendSms(String sms_service_url, String smsContent, String serialNumber, String... phoneNumbers) {
		String info = null;
		HashMap<String,String> result = new HashMap<String,String>();
		try{

			for(String tel : phoneNumbers){

				HttpClient httpclient = new HttpClient();
				PostMethod post = new PostMethod(sms_service_url);
				post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"UTF-8");
				post.addParameter("phoneNumber", tel);//手机号码
				post.addParameter("msgContent", smsContent);//短信消息
				post.addParameter("SerialNumber", serialNumber);//流水号，20位数字，唯一 （规则自定义,建议时间格式精确到毫秒）必填参数，不填后面无法使用回执接口。
				httpclient.executeMethod(post);

				info = new String(post.getResponseBody(),"UTF-8");
				result.put(tel, info);
				Thread.sleep(100l);
			}

		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
